﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DiscoveryLogic.UI.Layouts.Models;

namespace DiscoveryLogic.UI.Layouts.Controls
{

    public partial class Slider : UserControl
    {
        #region Public Variables

            private double barLeft;
            private double barWidth;

            private double thumbWidth;

            private double maxValue;
            private double minValue;

            private MouseManager mouseEventManager;

        #endregion

        #region Constructors

            public Slider() :
                this(285.7, 0, 100)
            {
            }

            public Slider(double sliderMinvalue, double sliderMaxValue):
                this(285.7, sliderMinvalue, sliderMaxValue)
            {
            }

            /// <summary>
            /// Creates a slider control
            /// </summary>
            /// <param name="sliderBarWidth">Width of the slider bar</param>
            /// <param name="sliderMinValue">Min value that the slider represents</param>
            /// <param name="sliderMaxValue">Max value that the slider represents</param>
            public Slider(double sliderBarWidth, double sliderMinValue, double sliderMaxValue)
            {
                // Required to initialize variables
                InitializeComponent();

                // Initialize Variables                
                barLeft = (double)SliderBar.GetValue(Canvas.LeftProperty);
                barWidth = sliderBarWidth;
                SliderBar.Width = barWidth;
                SliderBarWhite.Width = barWidth;
                minValue = sliderMinValue;
                maxValue = sliderMaxValue;            
                thumbWidth = SliderThumb.Width;

                mouseEventManager = new MouseManager("horizontal", barLeft, barWidth, thumbWidth);

                // Hook up event handlers
                SliderThumb.MouseEnter += mouseEventManager.OnMouseEnterHand;
                SliderThumb.MouseLeave += mouseEventManager.OnMouseLeaveArrow;
                SliderThumb.MouseLeftButtonDown += mouseEventManager.SliderMouseDown;
                SliderThumb.MouseLeftButtonUp += mouseEventManager.SliderMouseUp;
                SliderThumb.MouseMove += mouseEventManager.SliderMouseMove;

                mouseEventManager.sliderMove += new MouseManager.SliderMoveEventHandler(OnSliderThumbMove);
            }

        #endregion

        #region Public Properties

            public double MaxValue
            {
                get { return maxValue; }
                set { maxValue = value; }
            }

            public double MinValue
            {
                get { return minValue; }
                set { minValue = value; }
            }

        #endregion

        #region Public Methods

            /// <summary>
            /// Get the left position of the slider bar
            /// </summary>
            /// <returns></returns>
            public double GetSliderBarLeft()
            {
                return barLeft;
            }

            /// <summary>
            /// Get the width of the slider bar
            /// </summary>
            /// <returns></returns>
            public double GetSliderBarWidth()
            {
                return barWidth;
            }

            /// <summary>
            /// Get the width of the slider thumb
            /// </summary>
            /// <returns></returns>
            public double GetSliderThumbWidth()
            {
                return thumbWidth;
            }

            /// <summary>
            /// Show or hide the slider value label
            /// </summary>
            /// <param name="show"></param>
            public void ShowSliderLabel(bool show)
            {
                if (show == true)
                {
                    SliderLabel.Opacity = 1;
                }
                else
                {
                    SliderLabel.Opacity = 0;
                }
            }

            /// <summary>
            /// Set the slider and slider label when the
            /// mouse is moved. Also raises the event
            /// sliderThumbMoved.
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            public void OnSliderThumbMove(object sender, DiscoveryLogic.UI.Layouts.Models.CustomEventArgs e)
            {
                SliderThumb.SetValue(Canvas.LeftProperty, e.SliderThumbValue);
                SliderLabel.SetValue(Canvas.LeftProperty, e.SliderThumbValue);
                SliderLabel.Text = Math.Round(GetPositionInPercent() * 100) + " %";

                if (sliderThumbMoved != null)
                {
                    sliderThumbMoved(this, new EventArgs());
                }
            }

            /// <summary>
            /// Retrieve the distance of the slider thumb from the left
            /// in the form of a percentage.
            /// </summary>
            /// <returns></returns>
            public double GetPositionInPercent()
            {
                double position = ((double)SliderThumb.GetValue(Canvas.LeftProperty) + thumbWidth / 2)
                                  - barLeft;
                double percent = position / barWidth;

                return percent;
            }

            public double GetCurrentValue()
            {
                double percent = this.GetPositionInPercent();
                double currentValue = minValue + ((maxValue - minValue) * percent);

                return currentValue;
            }

            public void SetPosition(double value)
            {
                double length = maxValue - minValue;
                double percent = (value - minValue) / length;
                double left = barLeft + (barWidth * percent);
                SliderThumb.SetValue(Canvas.LeftProperty, left - (thumbWidth / 2));
                SliderLabel.SetValue(Canvas.LeftProperty, left - (thumbWidth / 2));
                SliderLabel.Text = Math.Round(GetPositionInPercent() * 100) + " %";
            }

        #endregion

        #region Events

            public event EventHandler sliderThumbMoved;

        #endregion
    }
}
